﻿using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using Mnk.Library.Common.Tools;
using Mnk.Library.Common.UI.Model;
using Mnk.Library.Common.UI.ModelsContainers;

namespace HappyBa.Core
{
    public class Connection
    {
        public string TargetProcessUrl { get; set; }
        public string LoginName { get; set; }
        public string Password { get; set; }
        public bool CollectOnStartup { get; set; }

        public Connection()
        {
            TargetProcessUrl = "https://targetprocess.com/";
            CollectOnStartup = false;
        }
    }

    public class Settings
    {
        public Connection Connection { get; set; }

        public Settings()
        {
            Connection = new Connection();
        }
    }

    public class Search
    {
        public CheckableDataCollection<CheckableData> Roles { get; set; }
        public CheckableDataCollection<CheckableData> States { get; set; }
        public CheckableDataCollection<CheckableData<int>> Users { get; set; }
        public CheckableDataCollection<CheckableData<int>> Releases { get; set; }

        public Search()
        {
            Roles = new CheckableDataCollection<CheckableData>();
            States = new CheckableDataCollection<CheckableData>();
            Users = new CheckableDataCollection<CheckableData<int>>();
            Releases = new CheckableDataCollection<CheckableData<int>>();
        }

        public Search Clone()
        {
            return new Search
            {
                Releases = Releases.Clone(),
                Roles = Roles.Clone(),
                Users = Users.Clone(),
                States = States.Clone()
            };
        }
    }

    public class Validation
    {
        public bool HideOldWarnings { get; set; }

        public Validation()
        {
            HideOldWarnings = true;
        }

        public Validation Clone()
        {
            return new Validation
            {
                HideOldWarnings = HideOldWarnings,
            };
        }
    }

    public class UsersAnalisys
    {
        public DateTime? DatesFrom { get; set; }
        public DateTime? DatesTo { get; set; }
        public bool ReportByDaysAndUsers { get; set; }
        public bool ReportByUsers { get; set; }
        public bool ReportTotal { get; set; }
        public bool Jobs { get; set; }
        public bool Tasks { get; set; }
        public bool GroupJobs { get; set; }
        public bool GroupTasks { get; set; }
        public bool RestrictTasksBySelectedInProfile { get; set; }

        public UsersAnalisys()
        {
            DatesFrom = DateTime.Today;
            DatesTo = DateTime.Today;
            ReportByDaysAndUsers = true;
            ReportByUsers = true;
            ReportTotal = true;
            Jobs = true;
            Tasks = true;
            GroupJobs = false;
            GroupTasks = false;
            RestrictTasksBySelectedInProfile = false;
        }

        public UsersAnalisys Clone()
        {
            return new UsersAnalisys
            {
                DatesFrom = DatesFrom,
                DatesTo = DatesTo,
                ReportByDaysAndUsers = ReportByDaysAndUsers,
                ReportByUsers = ReportByUsers,
                ReportTotal = ReportTotal,
                Jobs = Jobs,
                Tasks = Tasks,
                GroupJobs = GroupJobs,
                GroupTasks = GroupTasks,
                RestrictTasksBySelectedInProfile = RestrictTasksBySelectedInProfile
            };
        }
    }

    public enum ProjectType
    {
        Release,
        Feature,
        Iteration
    }

    public class Project : CheckableData
    {
        public DateTime PlannedEndDate { get; set; }
        public ProjectType Type { get; set; }

        public Project()
        {
            PlannedEndDate = DateTime.UtcNow;
            Type = ProjectType.Release;
        }

        public override object Clone()
        {
            return new Project
            {
                IsChecked = IsChecked,
                Key = Key,
                Type = Type
            };
        }
    }

    public class Profile : Mnk.Library.Common.UI.Model.Data
    {
        public Search Search { get; private set; }
        public Validation Validation { get; private set; }
        public UsersAnalisys UsersAnalisys { get; set; }
        public CheckableDataCollection<Project> Projects { get; set; }
        public ObservableCollection<Mnk.Library.Common.UI.Model.Data> ActiveEmployees { get; set; }

        public Profile()
        {
            Search = new Search();
            Validation = new Validation();
            UsersAnalisys = new UsersAnalisys();
            Projects = new CheckableDataCollection<Project>();
            ActiveEmployees = new ObservableCollection<Mnk.Library.Common.UI.Model.Data>();
        }

        public override object Clone()
        {
            return new Profile
            {
                Key = Key,
                Projects = Projects.Clone(),
                Search = Search.Clone(),
                Validation = Validation.Clone(),
                UsersAnalisys =  UsersAnalisys.Clone(),
                ActiveEmployees = ActiveEmployees.Clone(),
            };
        }
    }

    public class Config
    {
        public ObservableCollection<Profile> Profiles { get; set; }
        public Settings Settings { get; set; }
        public string SelectedProfile { get; set; }

        public Config()
        {
            Profiles = new ObservableCollection<Profile>();
            Settings = new Settings();
        }

        public Profile Current
        {
            get
            {
                return Profiles.FirstOrDefault(x => string.Equals(x.Key, SelectedProfile, StringComparison.OrdinalIgnoreCase));
            }
        }

        public string UserFolderName
        {
            get { return Path.GetInvalidPathChars().Aggregate(SelectedProfile, (str, x) => str.Replace(x, '_')); }
        }
    }
}
